const keyName = "angular-";

export const getParams = url => {
  // var url = window.location.search; //获取url中"?"符后的字串
  let theParams = new Object();
  // 判断是否有问号
  if (url.indexOf("?") != -1) {
    // 把问号去掉
    let str = url.substr(1);
    // 判断是否有多个参数
    if (str.indexOf("&") != -1) {
      // 以&符分项组成数组
      let strs = str.split("&");
      for (let i = 0; i < strs.length; i++) {
        // 循环数组
        // theParams[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        // 每一项等号左边为属性，等号右边为属性的值，值需要使用 decodeURI() 函数对通过 escape() 或 url 编码过的字符串进行解码。
        theParams[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
      }
    } else {
      // 只有一个参数
      let arr = str.split("=");
      theParams[arr[0]] = arr[1];
    }
  }
  return theParams;
};

/**
 * 存储localStorage
 */
export const setStore = (params = {}) => {
  let { name, content, type } = params;
  name = keyName + name;
  let obj = {
    dataType: typeof content,
    content: content,
    type: type,
    datetime: new Date().getTime()
  };
  if (type) window.sessionStorage.setItem(name, JSON.stringify(obj));
  else window.localStorage.setItem(name, JSON.stringify(obj));
};
/**
 * 获取localStorage
 */

export const getStore = (params = {}) => {
  let { name, debug } = params;
  name = keyName + name;
  let obj = {},
    content;
  obj = window.sessionStorage.getItem(name);
  if (validatenull(obj)) obj = window.localStorage.getItem(name);
  if (validatenull(obj)) return "";
  try {
    obj = JSON.parse(obj);
  } catch {
    return obj;
  }
  if (debug) {
    return obj;
  }
  if (obj.dataType == "string") {
    content = obj.content;
  } else if (obj.dataType == "number") {
    content = Number(obj.content);
  } else if (obj.dataType == "boolean") {
    content = Boolean(obj.content);
  } else if (obj.dataType == "object") {
    content = obj.content;
  }
  return content;
};
/**
 * 删除localStorage
 */
export const removeStore = (params = {}) => {
  let { name, type } = params;
  name = keyName + name;
  if (type) {
    window.sessionStorage.removeItem(name);
  } else {
    window.localStorage.removeItem(name);
  }
};

/**
 * 获取全部localStorage
 */
export const getAllStore = (params = {}) => {
  let list = [];
  let { type } = params;
  if (type) {
    for (let i = 0; i <= window.sessionStorage.length; i++) {
      list.push({
        name: window.sessionStorage.key(i),
        content: getStore({
          name: window.sessionStorage.key(i),
          type: "session"
        })
      });
    }
  } else {
    for (let i = 0; i <= window.localStorage.length; i++) {
      list.push({
        name: window.localStorage.key(i),
        content: getStore({
          name: window.localStorage.key(i)
        })
      });
    }
  }
  return list;
};

/**
 * 清空全部localStorage
 */
export const clearStore = (params = {}) => {
  let { type } = params;
  if (type) {
    window.sessionStorage.clear();
  } else {
    window.localStorage.clear();
  }
};

/**
 * 判断是否为空
 */
export function validatenull(val) {
  if (typeof val == "boolean") {
    return false;
  }
  if (typeof val == "number") {
    return false;
  }
  if (val instanceof Array) {
    if (val.length == 0) return true;
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === "{}") return true;
  } else {
    if (
      val == "null" ||
      val == null ||
      val == "undefined" ||
      val == undefined ||
      val == ""
    )
      return true;
    return false;
  }
  return false;
}

/**
 * 复制
 * @param value
 */
export const copyToClipboard = value => {
  const transfer = document.createElement("textarea");
  document.body.appendChild(transfer);
  transfer.value = value;
  transfer.focus();
  transfer.select();
  if (document.execCommand("copy")) {
    document.execCommand("copy");
  }
  transfer.blur();
  document.body.removeChild(transfer);
  return "复制成功！";
};

/**
 * 分享实现共享文件实现思路
 *  1. 需要 username 、文件id、filename 被邀请人（通常需要重新登录）
 */
export const createShareUrl = (username, fileid, filename) => {
  if (!fileid || !filename)
    return alert(`分享文件参数缺失: fileid=${fileid}, filename=${filename}`);

  // 解析路径
  let baseURL = window.location.origin;
  return `${baseURL}/invited?fileid=${fileid}&username=${username}&filename=${filename}`;
};
